package com.gnerv.battle.boot.module.base.model.bo;

import com.gnerv.battle.boot.module.base.model.entity.Permission;
import com.gnerv.battle.boot.common.base.exception.BusinessException;
import com.gnerv.battle.boot.common.tools.DateTimeTools;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpMethod;

import java.util.Arrays;
import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Data
public class PermissionBO {

    private static final List<String> TYPE_LIST = Arrays.stream(HttpMethod.values()).map(HttpMethod::name).toList();

    private String ukId;
    private String name;
    private String url;
    private String type;
    private String value;
    private String status;
    private Integer sort;

    public Permission toCreatePermission() {
        checkType();
        Permission permission = new Permission();
        BeanUtils.copyProperties(this, permission);
        permission.setGmtCreate(DateTimeTools.currentTimeMillis());
        permission.setGmtModified(DateTimeTools.currentTimeMillis());
        return permission;
    }

    public Permission toUpdatePermission() {
        checkType();
        Permission permission = new Permission();
        BeanUtils.copyProperties(this, permission);
        permission.setGmtModified(DateTimeTools.currentTimeMillis());
        return permission;
    }

    private void checkType(){
        if(!TYPE_LIST.contains(this.type)){
            throw new BusinessException("type's value must be one of " + TYPE_LIST);
        }
    }
}